package com.dfec.listerner;

import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.log4j.Logger;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.dfec.cache.redis.Cache;
import com.dfec.cache.redis.Redis;
import com.dfec.cache.redis.RedisModel;
import com.dfec.cache.redis.RedisPlugin;
import com.dfec.cache.redis.ToolCache;
import com.dfec.utils.DruidPlugin;
import com.dfec.utils.PropertiesUtil;


/**
 * Application Lifecycle Listener implementation class InitDruidListener
 *
 */
@WebListener
public class InitDruidListener implements ServletContextListener {
    static Logger log=null;
    DruidPlugin	druidPlugin;
    /**
     * Default constructor. 
     */
    public InitDruidListener() {
        // TODO Auto-generated constructor stub
    	log = Logger.getLogger(InitDruidListener.class.getName());
    	log.debug("InitListener Default constructor");
    	   // TODO Auto-generated method stub
    
    	if(log.isInfoEnabled()) log.info("configPlugin 配置Druid数据库连接池连接属性");
    	druidPlugin=new DruidPlugin(PropertiesUtil.JDBC_URL, PropertiesUtil.JDBC_USER,
    	    	PropertiesUtil.JDBC_PASS, PropertiesUtil.JDBC_DRIVER); 
		if(log.isInfoEnabled()) log.info("configPlugin 配置Druid数据库连接池大小");
		druidPlugin.set(PropertiesUtil.JDBC_INITIALSIZE, PropertiesUtil.JDBC_MINIDLE, PropertiesUtil.JDBC_MAXACTIVE);
		
		if(log.isInfoEnabled()) log.info("configPlugin 配置Druid数据库连接池过滤器配制");
		druidPlugin.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType(PropertiesUtil.type);
		WallConfig config = new WallConfig();
		config.setFunctionCheck(false); // 支持数据库函数
		wall.setConfig(config);
		druidPlugin.addFilter(wall);
		
//		if(log.isInfoEnabled()) log.info("EhCachePlugin EhCache缓存");
//		new EhCachePlugin().start();
		
	
		//
		if(PropertiesUtil.config_redis_use) {
			log.info(" Redis 缓存配置加载：开始");
			    RedisModel redis=new RedisModel();
			    redis.setIp(PropertiesUtil.config_redis_ip);
			    redis.setName(PropertiesUtil.config_redis_name);
			    redis.setPassword(PropertiesUtil.config_redis_password);
			    redis.setPort(PropertiesUtil.config_redis_port);
			    redis.setTimeout(PropertiesUtil.config_redis_timeout);
				RedisPlugin rp = new RedisPlugin(redis.getName(), redis.getIp(), redis.getPort(), redis.getTimeout(), redis.getPassword());
				rp.start();
		   log.info(" Redis 缓存配置加载：结束");
		}
		
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0)  { 
         // TODO Auto-generated method stub
    	druidPlugin.stop();
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0)  { 
	
		druidPlugin.start();
    }
	
}
